package org.caiotte.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.caiotte.domain.Department;
import org.caiotte.query.DepartmentQuery;
import org.caiotte.service.IDepartmentService;
import org.caiotte.util.AjaxResult;
import org.caiotte.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
*  部门管理接口
*    写接口三要素：
*      1.请求地址是什么
*      2.请求参数是什么
*      3.返回值是什么
*/

//@GetMapping("getById") = @RequestMapping(value="/getById",method = RequestMethod.GET)
//@PostMapping
//@PutMapping
//@PatchMapping
//@DeleteMapping
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //查询一条记录 用GET
    @GetMapping("/{id}")
    @ApiOperation(value = "基于id查询")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    //查询所有  用GET
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    //高级查询和分页 用POST
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        System.out.println(666);
        return  departmentService.queryPage(query);
    }

    //新增和修改 用PUT
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统错误 请稍后重试");
        }
    }

    //删除一条记录 用delete
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统错误 请稍后重试");
        }
    }

    /**
     *  批量删除
     *  前台传递的如果是数组   那么后台可以用list或者数组接收
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统错误  请稍后重试");
        }

    }

    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}
















